package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.NewsCases;
import com.ruoyi.system.service.INewsCasesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 案例信息Controller
 * 
 * @author shan
 * @date 2025-02-28
 */
@RestController
@RequestMapping("/system/cases")
public class NewsCasesController extends BaseController
{
    @Autowired
    private INewsCasesService newsCasesService;

    @Autowired
    private BotChatCompletionsExample botChatCompletionsExample;

    /**
     * 查询案例信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:cases:list')")
    @GetMapping("/list")
    public TableDataInfo list(NewsCases newsCases)
    {
        startPage();
        List<NewsCases> list = newsCasesService.selectNewsCasesList(newsCases);
        return getDataTable(list);
    }

    /**
     * 导出案例信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:cases:export')")
    @Log(title = "案例信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NewsCases newsCases)
    {
        List<NewsCases> list = newsCasesService.selectNewsCasesList(newsCases);
        ExcelUtil<NewsCases> util = new ExcelUtil<NewsCases>(NewsCases.class);
        util.exportExcel(response, list, "案例信息数据");
    }

    /**
     * 获取案例信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:cases:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(newsCasesService.selectNewsCasesById(id));
    }

    /**
     * 新增案例信息
     */
    @PreAuthorize("@ss.hasPermi('system:cases:add')")
    @Log(title = "案例信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NewsCases newsCases)
    {
        JSONObject news = botChatCompletionsExample.getNews(newsCases.getCaseName());
        if (news != null) {
            newsCases.setCaseCategory(news.getString("case_category"));
            newsCases.setCaseTime(news.getString("case_time"));
            newsCases.setSchool(news.getString("school"));
            newsCases.setKeywords(news.getString("keywords"));
            newsCases.setPersons(news.getString("persons"));
            newsCases.setOverview(news.getString("overview"));
            newsCases.setWarningLevel(news.getString("warning_level"));
        }
        newsCases.setCreateBy(getUsername());
        return toAjax(newsCasesService.insertNewsCases(newsCases));
    }

    /**
     * 修改案例信息
     */
    @PreAuthorize("@ss.hasPermi('system:cases:edit')")
    @Log(title = "案例信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NewsCases newsCases)
    {
        return toAjax(newsCasesService.updateNewsCases(newsCases));
    }

    /**
     * 删除案例信息
     */
    @PreAuthorize("@ss.hasPermi('system:cases:remove')")
    @Log(title = "案例信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(newsCasesService.deleteNewsCasesByIds(ids));
    }
}
